<?php


namespace app\api\business;


use app\api\server\Token;
use app\model\User;

class UserBusiness
{


    /**
     * 小程序授权登录
     */
    public static function authLogin($data){
        # 判断用户是否授权我们的小程序
        $res = User::where("openid",'=',$data['openid'])->find();

        if ($res) {
            User::update(['avatar' => $data['avatar'],'nickname' => $data['nickname'],'id' => $res->id]);
        } else {
            $res = User::create([
                'avatar' => $data['avatar'],
                'nickname' => $data['nickname'],
                'openid' => $data['openid']
            ]);
        }

        return [
            'token' => Token::createToken($res->id),
            'userInfo' => [
                'id' => $res->id,
                'name' => $res->username,
                'phone' => $res->phone,
                'nickname' => $res->nickname
            ],
        ];

    }
}